package com.example.anydo.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.Toast;

import androidx.core.app.NotificationCompat;

import com.example.anydo.R;
import com.example.anydo.activity.MainActivity;
import com.example.anydo.activity.TomatoAlarmActivity;

import static android.content.Context.NOTIFICATION_SERVICE;

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, TimeTaskService.class);
        Log.d("Alarm", "receiver");
        notice(context, intent);
        context.startService(i);
    }

    private void notice(Context context, Intent intent) {
        String noticeTitle = intent.getStringExtra("time");
        noticeTitle = noticeTitle.substring(0,2) + ":" + noticeTitle.substring(2,4);
        String noticeText = intent.getStringExtra("title");
        String channelId = "channel id";
        String channelName = "channel name";
        int id = 1;
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId);
        NotificationChannel channel;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

            channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(channel);
            builder.setChannelId(channelId);
        }

        // 启动 alarmActivity
        Intent appIntent = new Intent().setClass(context, TomatoAlarmActivity.class);
        appIntent.putExtra("title",noticeText);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, appIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        Notification notification = builder
                .setContentTitle(noticeTitle)
                .setContentText(noticeText)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pendingIntent)
                .setFullScreenIntent(pendingIntent,true)
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .build();
        notificationManager.notify(id, notification);
        Toast.makeText(context,"end time:"+System.currentTimeMillis(),Toast.LENGTH_SHORT).show();
        Log.d("time", "end time:"+System.currentTimeMillis());
    }
}
